<!DOCTYPE html>
<!-- 
	GeoEye Keynote Presentation Demonstration
	2011 Google Geospatial Awareness Day
	
	By: Alan Poole and Deke Young, GeoEye
	Date: August 24, 2011
 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<title>GeoEye Airport Mapping Database, 2011 Google Geospatial
	Awareness Day, GeoEye Keynote</title>
<script type="text/javascript"
	src="https://www.google.com/jsapi?key=ABQIAAAAMHzgPJvXPnl2p4bABkaCXhRimGqiYhdLemnilBW33CtvBu9uoxSq4N6qXZDmAg_SFP6A3nX6sZqFPg">
	
</script>
<script type="text/javascript">
	// Google Earth API instance, once loaded
	var ge = null;

	// load the Google Earth API
	google.load("earth", "1");

	// function to initialize and create a Google Earth API
	function initGlobe() {
		// create a new instance of the Google Earth API in the map3d div
		// call the initCallback function once loaded successfully
		// call the failureCallback if there is an issue creating an instance
		google.earth.createInstance("map3d", initCallback, failureCallback);
	}

	// function called once the Google Earth API instance is created
	function initCallback(object) {
		// set the ge objec tot he instance of the Google Earth API just created
		ge = object;

		// set the visibility of the instance to true
		ge.getWindow().setVisibility(true);

		// add the default/auto nativation controls
		ge.getNavigationControl().setVisibility(ge.VISIBILITY_AUTO);

		// add some default layers for navigation purposes
		ge.getLayerRoot().enableLayerById(ge.LAYER_BORDERS, true);
		ge.getLayerRoot().enableLayerById(ge.LAYER_ROADS, true);
		ge.getLayerRoot().enableLayerById(ge.LAYER_TERRAIN, true);

		// Get the current view
		var lookAt = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);

		// set the default location to the MROC airport, Costa Rica
		lookAt.setLatitude(9.993861111862087);
		lookAt.setLongitude(-84.20880555544869);
		lookAt.setRange(1000); // 1000 meters above the ground

		// Update the view in Google Earth
		ge.getView().setAbstractView(lookAt);

		// Add the Google Earth Builder side database/map service, by url
		// call the sideDbSucess function if the database was loaded successfully
		// call the sideDbFail function if the database was not loaded/added successfully
		// NOTE: This is an authorized service, for the demonstration, you must have already signed in.
		// No OAuth2 user sign-in support in this demonstration
		var server_url = "https://earthbuilder.google.com/16143158689603361093-17822223599500043129-4/kh/";
		server_url = server_url.replace(/^\s+|\s+$/g, '');
		google.earth.addSideDatabase(ge, server_url, sideDbSuccess, sideDbFail);
	}

	// function called when the Google Earth Builder side database is loaded/added succsessfully
	function sideDbSuccess(sideDb) {
		// grab the side database object
		var g_sideDb = sideDb;

		// set the drawing order of the database appropriately
		g_sideDb.setDrawOrder(2);

		// add the vectors from the database
		hVector(g_sideDb);
	}

	/* Code from */
	// TODO: Get source from Google Earth Builder Trusted Testers
	// This section is used to make sure the Earth Plug-In handles vector layers properly.
	// This might not be required when the Plug-In gets updated.
	var H_INTERVAL = 200;
	var hCount = 0;
	var hVectorIndexes = [];
	var hDb;

	function makeVectorsVisibleH() {
		if (hCount <= 0) {
			return;
		}

		hCount--;
		// Descend so we avoid problems with splice.
		var children = hDb.getFeatures().getChildNodes();
		for ( var i = hVectorIndexes.length - 1; i >= 0; i--) {
			var child = children.item(hVectorIndexes[i]).getFeatures()
					.getChildNodes().item(0);
			if (!child.getVisibility()) {
				child.setVisibility(true);
				hVectorIndexes.splice(i, i + 1);
			}
		}

		if (hVectorIndexes.length > 0) {
			setTimeout('makeVectorsVisibleH()', H_INTERVAL);
		}
	}

	function hVector(db) {
		this.db_ = db;

		hDb = db;
		// Only try for 3 seconds.
		hCount = 3000 / H_INTERVAL;
		hVectorIndexes = [];

		var children = this.db_.getFeatures().getChildNodes();
		for ( var i = 0; i < children.getLength(); i++) {
			var child = children.item(i).getFeatures().getChildNodes().item(0);
			if (child.getType() == 'KmlNetworkLink') {
				hVectorIndexes.push(i);
			}
		}

		if (hVectorIndexes.length > 0) {
			makeVectorsVisibleH();
		}
	}

	// function called when there is an issuea dding the side database to the instance
	function sideDbFail() {
		alert('Failed to add the earth builder database.');
	}

	// function called when there is an issue creating the Earth API instance
	function failureCallback(object) {
		alert("Google Earth globe could not be loaded.");
	}

	// add the initialize globe function to the on page load
	window.onload = initGlobe;
</script>
</head>

<body style="height: 100%; margin: 0; padding: 0;">
	<div id="map3d"
		style="border: 1px solid silver; height: 100%; width: 100%"></div>
</body>
</html>